#include "Animations.h"
#include "F3LampInterfaces.h"
#include "LampManager.h"
#include <string.h>

#define GO_Right false
#define GO_Left  truec

uint8_t g_LeturnLampChannelMaps[32] = {
    0x00, 0x01, 0x02,
    0x10, 0x11, 0x12,
    0x20, 0x21, 0x22,
    0x30, 0x31, 0x32,
    0x40, 0x41, 0x42,
    0x50, 0x51, 0x52,
    0x60, 0x61, 0x62,
    0x70, 0x71, 0x72,
    0x80, 0x81, 0x82,
    0x90, 0x91, 0x92,
    0xa0, 0xa1
};

struct LeFrntTurnLampFlickerAnimation g_LeFrntTurnLampFlickerAnimation = {
    .index = 0,
    .maxFrame = 64,
    .pwm = 100,
};

void playLeFrntTurnLampFlickerAnimation(uint32_t* outputFrame, float* pwm)
{
    const uint32_t max_frame = 32;
    if((g_LeFrntTurnLampFlickerAnimation.index++) / max_frame == 0)
    {
        *outputFrame = 0x000000;
    }
    else if((g_LeFrntTurnLampFlickerAnimation.index++) / (max_frame*2) == 0)
    {
        *outputFrame = 0xffffffff;
    }
    else
    {
        g_LeFrntTurnLampFlickerAnimation.index = 0;
    }
    *pwm = LeturnLamp_MaxCurrent;
}

struct LeFrntTurnLampFlowAnimation g_LeFrntTurnLampFlowAnimation =
{
    .index = 0,
    .maxFrame = 66,
};

void playLeFrntTurnLampFlowAnimation(uint32_t* outputFrame, float* pwm)
{
    const uint32_t speed = 1; // speed The higher the speed, the slower the speed
    uint32_t max_frame = 32*speed;
    if(g_LeFrntTurnLampFlowAnimation.index == 0 || g_LeFrntTurnLampFlowAnimation.index / max_frame == 0)
    {
        if(!GO_Right)
        {
            *outputFrame |= 0x01 << (g_LeFrntTurnLampFlowAnimation.index/speed);
        }
        else
        {
            *outputFrame |= 0x01 << (32-(g_LeFrntTurnLampFlowAnimation.index/speed)-1);
        }
        
        g_LeFrntTurnLampFlowAnimation.index++;
    }
    else
    {
        g_LeFrntTurnLampFlowAnimation.index = 0;
        *outputFrame = 0;
    }
    *pwm = LeturnLamp_MaxCurrent;
}

void trenLeFrntTurnLampOff(uint32_t* outputFrame, float* pwm)
{
    g_LeFrntTurnLampFlowAnimation.index = 0;
    g_LeFrntTurnLampFlickerAnimation.index = 0;
    *outputFrame = 0x0;
    *pwm = 0;
}


struct LeFrntTurnLampRibbonUnlock_Step_a g_LeFrntTurnLampRibbonUnlock_Step_a = {
    .index = 0,
};

void playLeFrntTurnLampRibbonUnlock_Step_a(void)
{
    if(g_LeFrntTurnLampRibbonUnlock_Step_a.index == 0)
    {
        g_LeFrntTrunLampView.model_->mask = 0x00;
        g_LeFrntTurnLampRibbonUnlock_Step_a.index++;
    }
    else if(g_LeFrntTurnLampRibbonUnlock_Step_a.index >= 1 && g_LeFrntTurnLampRibbonUnlock_Step_a.index < 33)
    {
        //Left turn lamp flow 0.2s
        if(GO_Right)
        {
            g_LeFrntTrunLampView.model_->mask |= 0x01 << (g_LeFrntTurnLampRibbonUnlock_Step_a.index-1);
        }
        else
        {
            g_LeFrntTrunLampView.model_->mask |= 0x01 << (32-(g_LeFrntTurnLampRibbonUnlock_Step_a.index));
        }
            g_LeFrntTurnLampRibbonUnlock_Step_a.index++;
        }
        else if(g_LeFrntTurnLampRibbonUnlock_Step_a.index >= 33 && g_LeFrntTurnLampRibbonUnlock_Step_a.index < 75)
        {
            //Left turn lamp turn on 0.2s
            g_LeFrntTrunLampView.model_->mask = 0xffffffff;
            g_LeFrntTurnLampRibbonUnlock_Step_a.index++;
        }
        else if(g_LeFrntTurnLampRibbonUnlock_Step_a.index >= 75 && g_LeFrntTurnLampRibbonUnlock_Step_a.index < 160)
        {
            //Left turn lamp turn off 0.4s
            g_LeFrntTrunLampView.model_->mask = 0x00000000;
            g_LeFrntTurnLampRibbonUnlock_Step_a.index++;
        }
        else
        {
        //Left turn lamp wait
        g_LeFrntTurnLampRibbonUnlock_Step_a.index = LeFrntTurnLampRibbonUnlock_Step_a_MaxFrame;
    }
    g_LeFrntTrunLampView.model_->pwm_ = DaytimeLampPwm_MaxCurrent;
}

bool isFinishLeFrntTurnLampRibbonUnlock_Step_a(void)
{
    return g_LeFrntTurnLampRibbonUnlock_Step_a.index >= LeFrntTurnLampRibbonUnlock_Step_a_MaxFrame;
}

void resetLeFrntTurnLampRibbonUnlock_Step_a(void)
{
    g_LeFrntTurnLampRibbonUnlock_Step_a.index = 0;
    g_LeFrntTrunLampView.model_->mask = 0x0;
    g_LeFrntTrunLampView.model_->pwm_ = 0.0f;
}

struct LeFrntTurnLampRibbonUnlock_Step_b g_LeFrntTurnLampRibbonUnlock_Step_b =
{
    .index = 0,
};

void playLeFrntTurnLampRibbonUnlock_Step_b(void)
{
    if(g_LeFrntTurnLampRibbonUnlock_Step_b.index == 0)
    {
        g_DayTimeAndPosLampView.model_->mask_ = 0x00;
        g_LeFrntTurnLampRibbonUnlock_Step_b.index++;
    }
    else if(g_LeFrntTurnLampRibbonUnlock_Step_b.index >= 1 && g_LeFrntTurnLampRibbonUnlock_Step_b.index < 33)
    {
        //Left turn lamp flow 0.2s
        if(GO_Right)
        {
            g_DayTimeAndPosLampView.model_->mask_ |= 0x01 << (g_LeFrntTurnLampRibbonUnlock_Step_b.index-1);
        }
        else
        {
            g_DayTimeAndPosLampView.model_->mask_ |= 0x01 << (32-(g_LeFrntTurnLampRibbonUnlock_Step_b.index));
        }
        g_LeFrntTurnLampRibbonUnlock_Step_b.index++;
    }
    else if(g_LeFrntTurnLampRibbonUnlock_Step_b.index >= 33 && g_LeFrntTurnLampRibbonUnlock_Step_b.index < 40)
    {
        g_LeFrntTurnLampRibbonUnlock_Step_b.index++;
    }
    else
    {
        //Left turn lamp wait
        g_LeFrntTurnLampRibbonUnlock_Step_b.index = LeFrntTurnLampRibbonUnlock_Step_b_MaxFrame;
    }
    g_DayTimeAndPosLampView.model_->pwm_ = getPosnBriSet();
}

bool isFinishLeFrntTurnLampRibbonUnlock_Step_b(void)
{
    return g_LeFrntTurnLampRibbonUnlock_Step_b.index >= LeFrntTurnLampRibbonUnlock_Step_b_MaxFrame;
}

void resetLeFrntTurnLampRibbonUnlock_Step_b(void)
{
    g_LeFrntTurnLampRibbonUnlock_Step_b.index = 0;
    g_DayTimeAndPosLampView.model_->mask_ = 0x0;
    g_DayTimeAndPosLampView.model_->pwm_ = 0.0f;
}

struct LeFrntTurnLampRibbonUnlock_Step_c g_LeFrntTurnLampRibbonUnlock_Step_c =
{
    .index = 0,
};
void playLeFrntTurnLampRibbonUnlock_Step_c(void)
{

    if(g_LeFrntTurnLampRibbonUnlock_Step_c.index < LeFrntTurnLampRibbonUnlock_Step_c_MaxFrame)
    {
        if(GO_Right)
        {
            g_StarrySkyView.model_->mask_ |= 0x01 << (g_LeFrntTurnLampRibbonUnlock_Step_c.index/40);
        }
        else
        {
            g_StarrySkyView.model_->mask_ |= 0x01 << (9-(g_LeFrntTurnLampRibbonUnlock_Step_c.index/40));
        }
        g_StarrySkyView.model_->pwm_ = getStarrySkyBriSet();
        g_LeFrntTurnLampRibbonUnlock_Step_c.index++;
    }
    else
    {
        g_LeFrntTurnLampRibbonUnlock_Step_c.index = LeFrntTurnLampRibbonUnlock_Step_c_MaxFrame;
    }
}

bool isFinishLeFrntTurnLampRibbonUnlock_Step_c(void)
{
    return g_LeFrntTurnLampRibbonUnlock_Step_c.index >= LeFrntTurnLampRibbonUnlock_Step_c_MaxFrame;
}

void resetLeFrntTurnLampRibbonUnlock_Step_c(void)
{
    g_LeFrntTurnLampRibbonUnlock_Step_c.index = 0;
    g_StarrySkyView.model_->mask_ = 0x0;
    g_StarrySkyView.model_->pwm_ = 0.0f;
}

struct LoBeamRibbonUnlock_Step_d_1 g_LoBeamRibbonUnlock_Step_d_1 = {
    .index = 0,
};

void playLoBeamRibbonUnlock_Step_d_1(void)
{
    const uint8_t base_pwm = 36;
    if(g_LoBeamRibbonUnlock_Step_d_1.index < LoBeamRibbonUnlock_Step_d_1_MaxFrame)
    {
        g_LoBeamView.model_->pwm_ = base_pwm + g_LoBeamRibbonUnlock_Step_d_1.index;
    }
    g_LoBeamRibbonUnlock_Step_d_1.index++;
    g_LoBeamRibbonUnlock_Step_d_1.index = g_LoBeamRibbonUnlock_Step_d_1.index >= LoBeamRibbonUnlock_Step_d_1_MaxFrame ? LoBeamRibbonUnlock_Step_d_1_MaxFrame : g_LoBeamRibbonUnlock_Step_d_1.index;
    g_LoBeamView.model_->onOffStatus_ = OnOff_On;

}

bool isFinishLoBeamRibbonUnlock_Step_d_1(void)
{
    return g_LoBeamRibbonUnlock_Step_d_1.index >= LoBeamRibbonUnlock_Step_d_1_MaxFrame;
}

void resetLoBeamRibbonUnlock_Step_d_1(void)
{
    g_LoBeamRibbonUnlock_Step_d_1.index = 0;
    g_LoBeamView.model_->onOffStatus_ = OnOff_Off;
    g_LoBeamView.model_->pwm_ = 0.0f;
}

struct LoBeamRibbonUnlock_Step_d_2 g_LoBeamRibbonUnlock_Step_d_2 = {
    .index = 0,
};

void playLoBeamRibbonUnlock_Step_d_2(void)
{
    const uint8_t base_pwm = 35;
    if(g_LoBeamRibbonUnlock_Step_d_2.index < LoBeamRibbonUnlock_Step_d_2_MaxFrame)
    {
        g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = base_pwm + (g_LoBeamRibbonUnlock_Step_d_2.index);
    }
    g_LoBeamRibbonUnlock_Step_d_2.index++;
    g_LoBeamRibbonUnlock_Step_d_2.index = g_LoBeamRibbonUnlock_Step_d_2.index >= LoBeamRibbonUnlock_Step_d_2_MaxFrame ? LoBeamRibbonUnlock_Step_d_2_MaxFrame : g_LoBeamRibbonUnlock_Step_d_2.index;
    g_LoBeamView.model_->onOffStatus_ = OnOff_On;
}

bool isFinishLoBeamRibbonUnlock_Step_d_2(void)
{
    return g_LoBeamRibbonUnlock_Step_d_2.index >= LoBeamRibbonUnlock_Step_d_2_MaxFrame;
}

void resetLoBeamRibbonUnlock_Step_d_2(void)
{
    g_LoBeamRibbonUnlock_Step_d_2.index = 0;
    g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 0;
    g_DlpModelView.model_->isLM3409_EN_Enable_ = false;
    g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = false;
}

struct LoBeamRibbonUnlock_Step_d_3 g_LoBeamRibbonUnlock_Step_d_3 = {
    .index = 0,
};

void playLoBeamRibbonUnlock_Step_d_3(void)
{
    const uint8_t lobeamBase = 100;
    const uint16_t dlpBase = 99;
    if(g_LoBeamRibbonUnlock_Step_d_3.index < LoBeamRibbonUnlock_Step_d_3_MaxFrame)
    {
        g_LoBeamView.model_->pwm_ = lobeamBase - (g_LoBeamRibbonUnlock_Step_d_3.index/2);
        g_LoBeamView.model_->onOffStatus_ = OnOff_On;

        g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = dlpBase - (g_LoBeamRibbonUnlock_Step_d_3.index/2);
        g_DlpModelView.model_->isLM3409_EN_Enable_ = true;
        g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = true;

        g_LoBeamRibbonUnlock_Step_d_3.index++;
    }
    else
    {
        g_LoBeamRibbonUnlock_Step_d_3.index = LoBeamRibbonUnlock_Step_d_3_MaxFrame;
    }
}

bool isFinishLoBeamRibbonUnlock_Step_d_3(void)
{
    return g_LoBeamRibbonUnlock_Step_d_3.index >= LoBeamRibbonUnlock_Step_d_3_MaxFrame;
}

void resetLoBeamRibbonUnlock_Step_d_3(void)
{
    g_LoBeamRibbonUnlock_Step_d_3.index = 0;
    g_LoBeamView.model_->onOffStatus_ = OnOff_Off;
    g_LoBeamView.model_->pwm_ = 0.0f;
    g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 0;
    g_DlpModelView.model_->isLM3409_EN_Enable_ = false;
    g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = false;
}

struct LoBeamLampRibbonLock_Step_c_1 g_LoBeamLampRibbonLock_Step_c_1 = {
    .index = 0
};

void playLoBeamLampRibbonLock_Step_c_1(void)
{
    if(g_LoBeamLampRibbonLock_Step_c_1.index < LoBeamLampRibbonLock_Step_c_1MaxFrame)
    {
        uint16_t value = g_LoBeamLampRibbonLock_Step_c_1.index;
        value = value > 50 ? 50 : value;
        g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 50 - value;
        g_DlpModelView.model_->isLM3409_EN_Enable_ = true;
        g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = true;
        g_LoBeamLampRibbonLock_Step_c_1.index++;
    }
    else
    {
        g_LoBeamLampRibbonLock_Step_c_1.index = LoBeamLampRibbonLock_Step_c_1MaxFrame;
    }
}

bool isFinishLoBeamLampRibbonLock_Step_c_1(void)
{
    return g_LoBeamLampRibbonLock_Step_c_1.index >= LoBeamLampRibbonLock_Step_c_1MaxFrame;
}

void resetLoBeamLampRibbonLock_Step_c_1(void)
{
    g_LoBeamLampRibbonLock_Step_c_1.index = 0;
    g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 0;
    g_DlpModelView.model_->isLM3409_EN_Enable_ = false;
    g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = false;
}

struct LoBeamLampRibbonLock_Step_c_2 g_LoBeamLampRibbonLock_Step_c_2 = {
    .index = 0,
};
void playLoBeamLampRibbonLock_Step_c_2(void)
{
    if(g_LoBeamLampRibbonLock_Step_c_2.index < LoBeamLampRibbonLock_Step_c_1MaxFrame)
    {
        float value = g_LoBeamLampRibbonLock_Step_c_2.index / 2;
        value = value > 50.0 ? 50.0 : value;
        g_LoBeamView.model_->pwm_ = 50.0f - value;
        g_LoBeamView.model_->onOffStatus_ = OnOff_On;
        g_LoBeamLampRibbonLock_Step_c_2.index++;
    }
    else
    {
        g_LoBeamLampRibbonLock_Step_c_2.index = LoBeamLampRibbonLock_Step_c_2MaxFrame;
    }
}

bool isFinishLoBeamLampRibbonLock_Step_c_2(void)
{
    return g_LoBeamLampRibbonLock_Step_c_2.index >= LoBeamLampRibbonLock_Step_c_2MaxFrame;
}

void resetLoBeamLampRibbonLock_Step_c_2(void)
{
    g_LoBeamLampRibbonLock_Step_c_2.index = 0;
    g_LoBeamView.model_->onOffStatus_ = OnOff_Off;
    g_LoBeamView.model_->pwm_ = 0.0f;
}


struct LeFrntTurnLampRibbonLock_Step_d g_LeFrntTurnLampRibbonLock_Step_d = {
    .index = 0
};
void playLeFrntTurnLampRibbonLock_Step_d(void)
{
    const uint32_t base = 0x3ff;
    if(g_LeFrntTurnLampRibbonLock_Step_d.index < LeFrntTurnLampRibbonLock_Step_dMaxFrame)
    {
        if(g_LeFrntTurnLampRibbonLock_Step_d.index == 0)
        {
            g_StarrySkyView.model_->mask_ = 0x3ff;
        }

        if(!GO_Right)
        {
            g_StarrySkyView.model_->mask_ &= base >> (g_LeFrntTurnLampRibbonLock_Step_d.index/29);
        }
        else
        {
            g_StarrySkyView.model_->mask_ &= (base << (g_LeFrntTurnLampRibbonLock_Step_d.index/29)) & 0xffffffff;
        }
        g_StarrySkyView.model_->pwm_ = getStarrySkyBriSet();
        g_LeFrntTurnLampRibbonLock_Step_d.index++;
    }
    else
    {
        g_LeFrntTurnLampRibbonLock_Step_d.index = LeFrntTurnLampRibbonUnlock_Step_c_MaxFrame;
    }
}

bool isFinishLeFrntTurnLampRibbonLock_Step_d(void)
{
    return g_LeFrntTurnLampRibbonLock_Step_d.index >= LeFrntTurnLampRibbonLock_Step_dMaxFrame;
}

void resetLeFrntTurnLampRibbonLock_Step_d(void)
{
    g_LeFrntTurnLampRibbonLock_Step_d.index = 0;
    //     g_StarrySkyView.model_->mask_ = 0x3ff;
    //     g_StarrySkyView.model_->pwm_ = 100;
}


struct LeFrntTurnLampRibbonLock_Step_e g_LeFrntTurnLampRibbonLock_Step_e = {
    .index = 0,
};
void playLeFrntTurnLampRibbonLock_Step_e(void)
{
    if(g_LeFrntTurnLampRibbonLock_Step_e.index < LeFrntTurnLampRibbonLock_Step_eMaxFrame)
    {
        const uint64_t base = 0xffffffff;
        if(g_LeFrntTurnLampRibbonLock_Step_e.index == 0)
        {
            g_DayTimeAndPosLampView.model_->mask_ = 0xffffffff;
        }

        if(GO_Right)
        {
            g_DayTimeAndPosLampView.model_->mask_ &= base >> g_LeFrntTurnLampRibbonLock_Step_e.index;
        }
        else
        {
            g_DayTimeAndPosLampView.model_->mask_ &= base << g_LeFrntTurnLampRibbonLock_Step_e.index;
        }
        g_DayTimeAndPosLampView.model_->pwm_ = getPosnBriSet();
        g_LeFrntTurnLampRibbonLock_Step_e.index++;
    }
    else
    {
        g_LeFrntTurnLampRibbonLock_Step_e.index = LeFrntTurnLampRibbonLock_Step_eMaxFrame;
    }
}

bool isFinishLeFrntTurnLampRibbonLock_Step_e(void)
{
    return g_LeFrntTurnLampRibbonLock_Step_e.index >= LeFrntTurnLampRibbonLock_Step_eMaxFrame;
}

void resetLeFrntTurnLampRibbonLock_Step_e(void)
{
    g_LeFrntTurnLampRibbonLock_Step_e.index = 0;
    g_DayTimeAndPosLampView.model_->mask_ = 0x0;
    g_DayTimeAndPosLampView.model_->pwm_ = 0;
}

static void playRibbonHangup(HangupAction* action)
{
    action->index++;
    if(action->index >= action->maxFrame)
    {
        action->index = action->maxFrame;
    }
}

static bool isFinishRibbonHangup(HangupAction* action)
{
    return action->index >= action->maxFrame;
}

static void resetRibbonHangup(HangupAction* action)
{
    action->index = 0;
}

HangupAction g_RibbonHangup_160ms = {
    .index = 0,
    .maxFrame = 32,
};
void playRibbonHangup_160ms(void)
{
    playRibbonHangup(&g_RibbonHangup_160ms);
}

bool isFinishRibbonHangup_160ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_160ms);
}

void resetRibbonHangup_160ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_160ms);
}

HangupAction g_RibbonHangup_560ms = {
    .index = 0,
    .maxFrame = 112,
};
void playRibbonHangup_560ms(void)
{
    playRibbonHangup(&g_RibbonHangup_560ms);
}

bool isFinishRibbonHangup_560ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_560ms);
}

void resetRibbonHangup_560ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_560ms);
}


HangupAction g_RibbonHangup_800ms = {
    .index = 0,
    .maxFrame = 160,
};
void playRibbonHangup_800ms(void)
{
    playRibbonHangup(&g_RibbonHangup_800ms);
}

bool isFinishRibbonHangup_800ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_800ms);
}

void resetRibbonHangup_800ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_800ms);
}

HangupAction g_RibbonHangup_1000ms = {
    .index = 0,
    .maxFrame = 200,
};
void playRibbonHangup_1000ms(void)
{
    playRibbonHangup(&g_RibbonHangup_1000ms);
}

bool isFinishRibbonHangup_1000ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_1000ms);
}

void resetRibbonHangup_1000ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_1000ms);
}

HangupAction g_RibbonHangup_1200ms = {
    .index = 0,
    .maxFrame = 240,
};
void playRibbonHangup_1200ms(void)
{
    playRibbonHangup(&g_RibbonHangup_1200ms);
}

bool isFinishRibbonHangup_1200ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_1200ms);
}

void resetRibbonHangup_1200ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_1200ms);
}

HangupAction g_RibbonHangup_1680ms = {
    .index = 0,
    .maxFrame = 336
};

void playRibbonHangup_1680ms(void)
{
    playRibbonHangup(&g_RibbonHangup_1680ms);
}

bool isFinishRibbonHangup_1680ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_1680ms);
}

void resetRibbonHangup_1680ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_1680ms);
}



HangupAction g_RibbonHangup_4600ms =
{
    .index = 0,
    .maxFrame = 920,
};
void playRibbonHangup_4600ms(void)
{
    playRibbonHangup(&g_RibbonHangup_4600ms);
}

bool isFinishRibbonHangup_4600ms(void)
{
    return isFinishRibbonHangup(&g_RibbonHangup_4600ms);
}

void resetRibbonHangup_4600ms(void)
{
    resetRibbonHangup(&g_RibbonHangup_4600ms);
}

LeFrntTurnLampRibbonUnlockAnimationManager g_LeFrntTurnLampRibbonUnlockAnimationManager = {
    .animationIndex_ = 0,
    .isAnimationFinish_ = false,
};
void initLeFrntTurnLampRibbonUnlockAnimationManager(void)
{
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[0] = playLeFrntTurnLampRibbonUnlock_Step_a;        //160 frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[1] = playLeFrntTurnLampRibbonUnlock_Step_a;        //160 frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[2] = playRibbonHangup_800ms;                       //160 frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[3] = playLeFrntTurnLampRibbonUnlock_Step_b;        //40  frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[4] = playLeFrntTurnLampRibbonUnlock_Step_c;        //400 frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[5] = playRibbonHangup_1200ms;                      //240 frames
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[0] = isFinishLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[1] = isFinishLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[2] = isFinishRibbonHangup_800ms;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[3] = isFinishLeFrntTurnLampRibbonUnlock_Step_b;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[4] = isFinishLeFrntTurnLampRibbonUnlock_Step_c;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[5] = isFinishRibbonHangup_1200ms;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[0] = resetLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[1] = resetLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[2] = resetRibbonHangup_800ms;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[3] = resetLeFrntTurnLampRibbonUnlock_Step_b;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[4] = resetLeFrntTurnLampRibbonUnlock_Step_c;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[5] = resetRibbonHangup_1200ms;

}

void playLeFrntTurnLampRibbonUnlockAnimation(void)
{
    if(g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_ < LeFrntTurnLampRibbonUnlockMaxStemp)
    {
        g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Running;
        g_LeFrntTurnLampRibbonUnlockAnimationManager.animationFunctions[g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_]();
        if(g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_]())
        {
            g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_++;
            if(g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_ == 1)
            {
                // step a play twics, so init flag between first time and second time
                g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_]();
            }

            if(g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_ >= LeFrntTurnLampRibbonUnlockMaxStemp)
            {
                g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinish_ = true;
            }
        }
    }
    else
    {
        g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Hold;
    }
}

void repeatLeFrntTurnLampRibbonUnlockAnimation(void)
{
    if(isLeFrntTurnLampRibbonUnlockAnimationFinish())
    {
        resetLeFrntTurnLampRibbonUnlockAnimation();
    }
}

bool isLeFrntTurnLampRibbonUnlockAnimationFinish(void)
{
    return g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinish_;
}
bool isLeFrntTurnLampRibbonUnlockAnimationPlaying(void)
{
    return g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_ > 0 || g_LeFrntTurnLampRibbonUnlock_Step_a.index > 0;
}

void resetLeFrntTurnLampRibbonUnlockAnimation(void)
{
    g_LeFrntTurnLampRibbonUnlockAnimationManager.isAnimationFinish_ = false;
    g_LeFrntTurnLampRibbonUnlockAnimationManager.animationIndex_ = 0;
    for(int i = 0; i < LeFrntTurnLampRibbonUnlockMaxStemp; i++)
    {
        g_LeFrntTurnLampRibbonUnlockAnimationManager.resetAnimationFunctions[i]();
    }
}

void justCloseRibbonLeFrntTurnLamp(void)
{
    g_DayTimeAndPosLampView.model_->mask_ = 0x0;
    g_DayTimeAndPosLampView.model_->pwm_ = 0;
    g_StarrySkyView.model_->mask_ = 0x0;
    g_StarrySkyView.model_->pwm_ = 0;
    g_LeFrntTrunLampView.model_->mask = 0x0;
    g_LeFrntTrunLampView.model_->pwm_ = 0.0;
}

LobeamLampRibbonUnlockAnimationManager g_LobeamLampRibbonUnlockAnimationManager = {
    .animationIndex_ = 0,
    .isAnimationFinish_ = false,
};
void initLobeamLampRibbonUnlockAnimationManager(void)
{
    g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[0] = playRibbonHangup_4600ms;               //920 frames
    g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[1] = playLoBeamRibbonUnlock_Step_d_1;      //64
    g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[2] = playLoBeamRibbonUnlock_Step_d_2;      //64
    g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[3] = playRibbonHangup_160ms;               //32
    g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[4] = playLoBeamRibbonUnlock_Step_d_3;      //80
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[0] = isFinishRibbonHangup_4600ms;
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[1] = isFinishLoBeamRibbonUnlock_Step_d_1;
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[2] = isFinishLoBeamRibbonUnlock_Step_d_2;
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[3] = isFinishRibbonHangup_160ms;
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[4] = isFinishLoBeamRibbonUnlock_Step_d_3;
    g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[0] = resetRibbonHangup_4600ms;
    g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[1] = resetLoBeamRibbonUnlock_Step_d_1;
    g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[2] = resetLoBeamRibbonUnlock_Step_d_2;
    g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[3] = resetRibbonHangup_160ms;
    g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[4] = resetLoBeamRibbonUnlock_Step_d_3;
}

void playLobeamLampRibbonUnlockAnimation(void)
{
    if(g_LobeamLampRibbonUnlockAnimationManager.animationIndex_ < LobeamLampRibbonUnlockMaxStemp)
    {
        g_RibbonView.model_->loBeamEffectShowSt_ = RibbonEffectShow_Running;    //set can signal
        g_LobeamLampRibbonUnlockAnimationManager.animationFunctions[g_LobeamLampRibbonUnlockAnimationManager.animationIndex_]();
        if(g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinishFunctions[g_LobeamLampRibbonUnlockAnimationManager.animationIndex_]())
        {
            g_LobeamLampRibbonUnlockAnimationManager.animationIndex_++;
            if(g_LobeamLampRibbonUnlockAnimationManager.animationIndex_ >= LobeamLampRibbonUnlockMaxStemp)
            {
                g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinish_ = true;
            }
        }
    }
    else
    {
        g_RibbonView.model_->loBeamEffectShowSt_ = RibbonEffectShow_Hold;
    }
}

void repeatLobeamLampRibbonUnlockAnimation(void)
{
    if(isLobeamLampRibbonUnlockAnimationFinish())
    {
        resetLobeamLampRibbonUnlockAnimation();
    }
}


bool isLobeamLampRibbonUnlockAnimationFinish(void)
{
    return g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinish_;
}

void resetLobeamLampRibbonUnlockAnimation(void)
{
    g_LobeamLampRibbonUnlockAnimationManager.isAnimationFinish_ = false;
    g_LobeamLampRibbonUnlockAnimationManager.animationIndex_ = 0;
    for(int i = 0; i < LobeamLampRibbonUnlockMaxStemp; i++)
    {
        g_LobeamLampRibbonUnlockAnimationManager.resetAnimationFunctions[i]();
    }
}

LeFrntTurnLampRibbonLockAnimationManager g_LeFrntTurnLampRibbonLockAnimationManager = {
    .animationIndex_ = 0,
    .isAnimationFinish_ = false,
};

void initLeFrntTurnLampRibbonLockAnimationManager(void)
{
    g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[0] = playLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[1] = playLeFrntTurnLampRibbonUnlock_Step_b;
    g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[2] = playRibbonHangup_1200ms;
    g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[3] = playLeFrntTurnLampRibbonLock_Step_d;
    g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[4] = playLeFrntTurnLampRibbonLock_Step_e;
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[0] = isFinishLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[1] = isFinishLeFrntTurnLampRibbonUnlock_Step_b;
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[2] = isFinishRibbonHangup_1200ms;
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[3] = isFinishLeFrntTurnLampRibbonLock_Step_d;
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[4] = isFinishLeFrntTurnLampRibbonLock_Step_e;
    g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[0] = resetLeFrntTurnLampRibbonUnlock_Step_a;
    g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[1] = resetLeFrntTurnLampRibbonUnlock_Step_b;
    g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[2] = resetRibbonHangup_1200ms;
    g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[3] = resetLeFrntTurnLampRibbonLock_Step_d;
    g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[4] = resetLeFrntTurnLampRibbonLock_Step_e;
}

void playLeFrntTurnLampRibbonLockAnimation(void)
{
    if(g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_ < LeFrntTurnLampRibbonLockMaxStemp)
    {
        g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Running;
        g_LeFrntTurnLampRibbonLockAnimationManager.animationFunctions[g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_]();
        if(g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinishFunctions[g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_]())
        {
            g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_++;
            if(g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_ >= LeFrntTurnLampRibbonLockMaxStemp)
            {
                g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinish_ = true;
            }
        }
    }
    else
    {
        g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Hold;
    }
}

void repeatLeFrntTurnLampRibbonLockAnimation(void)
{
    if(isLeFrntTurnLampRibbonLockAnimationFinish())
    {
        resetLeFrntTurnLampRibbonLockAnimation();
    }
}

bool isLeFrntTurnLampRibbonLockAnimationFinish(void)
{
    return g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinish_;
}

bool isLeFrntTurnLampRibbonLockAnimationPlaying(void)
{
    return g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_ > 0 || g_LeFrntTurnLampRibbonUnlock_Step_a.index > 0;
}

void resetLeFrntTurnLampRibbonLockAnimation(void)
{
    g_LeFrntTurnLampRibbonLockAnimationManager.isAnimationFinish_ = false;
    g_LeFrntTurnLampRibbonLockAnimationManager.animationIndex_ = 0;
    for(int i = 0; i < LeFrntTurnLampRibbonLockMaxStemp; i++)
    {
        g_LeFrntTurnLampRibbonLockAnimationManager.resetAnimationFunctions[i]();
    }
}

LobeamLampRibbonLockAnimationManager g_LobeamLampRibbonLockAnimationManager = {
    .animationIndex_ = 0,
    .isAnimationFinish_ = false,
};

void initLobeamLampRibbonLockAnimationManager(void)
{
    g_LobeamLampRibbonLockAnimationManager.animationFunctions[0] = playRibbonHangup_1000ms;
    g_LobeamLampRibbonLockAnimationManager.animationFunctions[1] = playLoBeamLampRibbonLock_Step_c_1;
    g_LobeamLampRibbonLockAnimationManager.animationFunctions[2] = playLoBeamLampRibbonLock_Step_c_2;
    g_LobeamLampRibbonLockAnimationManager.animationFunctions[3] = playRibbonHangup_1680ms;
    g_LobeamLampRibbonLockAnimationManager.isAnimationFinishFunctions[0] = isFinishRibbonHangup_1000ms;
    g_LobeamLampRibbonLockAnimationManager.isAnimationFinishFunctions[1] = isFinishLoBeamLampRibbonLock_Step_c_1;
    g_LobeamLampRibbonLockAnimationManager.isAnimationFinishFunctions[2] = isFinishLoBeamLampRibbonLock_Step_c_2;
    g_LobeamLampRibbonLockAnimationManager.isAnimationFinishFunctions[3] = isFinishRibbonHangup_1680ms;
    g_LobeamLampRibbonLockAnimationManager.resetAnimationFunctions[0] = resetRibbonHangup_1000ms;
    g_LobeamLampRibbonLockAnimationManager.resetAnimationFunctions[1] = resetLoBeamLampRibbonLock_Step_c_1;
    g_LobeamLampRibbonLockAnimationManager.resetAnimationFunctions[2] = resetLoBeamLampRibbonLock_Step_c_2;
    g_LobeamLampRibbonLockAnimationManager.resetAnimationFunctions[3] = resetRibbonHangup_1680ms;
}

void playLobeamLampRibbonLockAnimation(void)
{
    // turn on lobeam and dlp first

    if(g_LobeamLampRibbonLockAnimationManager.animationIndex_ == 0 && (OnOff_Off == g_LoBeamView.model_->onOffStatus_ || !g_DlpModelView.model_->isLM3409_EN_Enable_))
    {
        g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 50;
        g_DlpModelView.model_->isLM3409_EN_Enable_ = true;
        g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = true;
        g_LoBeamView.model_->pwm_ = 50.0f;
        g_LoBeamView.model_->onOffStatus_ = OnOff_On;
    }
    if(g_LobeamLampRibbonLockAnimationManager.animationIndex_ < LobeamLampRibbonLockMaxStemp)
    {    
        g_RibbonView.model_->loBeamEffectShowSt_ = RibbonEffectShow_Running;
        g_LobeamLampRibbonLockAnimationManager.animationFunctions[g_LobeamLampRibbonLockAnimationManager.animationIndex_]();
        if(g_LobeamLampRibbonLockAnimationManager.isAnimationFinishFunctions[g_LobeamLampRibbonLockAnimationManager.animationIndex_]())
        {
            g_LobeamLampRibbonLockAnimationManager.animationIndex_++;
            if(g_LobeamLampRibbonLockAnimationManager.animationIndex_ >= LobeamLampRibbonLockMaxStemp)
            {
                g_LobeamLampRibbonLockAnimationManager.isAnimationFinish_ = true;
            }
        }
    }
    else
    {
        g_RibbonView.model_->loBeamEffectShowSt_ = RibbonEffectShow_Hold;
    }
}

void repeatLobeamLampRibbonLockAnimation(void)
{
    if(isLobeamLampRibbonLockAnimationFinish())
    {
        resetLobeamLampRibbonLockAnimation();
    }
}

bool isLobeamLampRibbonLockAnimationFinish(void)
{
    return g_LobeamLampRibbonLockAnimationManager.isAnimationFinish_;
}

void resetLobeamLampRibbonLockAnimation(void)
{
    g_LobeamLampRibbonLockAnimationManager.isAnimationFinish_ = false;
    g_LobeamLampRibbonLockAnimationManager.animationIndex_ = 0;
    for(int i = 0; i < LobeamLampRibbonLockMaxStemp; i++)
    {
        g_LobeamLampRibbonLockAnimationManager.resetAnimationFunctions[i]();
    }
}

